﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using VirtualPress.Tool.Framework;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    /// <summary>
    /// 附件
    /// </summary>
    [Table("T_Appendix")]
    public class T_Appendix : IEntity<T_Appendix>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        /// <summary>
        /// 关联ID 
        /// type = 1 稿件的附件 关联稿件id
        /// type = 2 审搞单    关联审稿记录id
        /// type = 3 专家推荐材料 关联稿件id
        /// </summary>
        public int RaletionId { get; set; }
        /// <summary>
        /// 拥有者
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string Own { get; set; }
        [Column(TypeName="nvarchar")]
        [StringLength(256)]
        public string FileName { get; set; }
        /// <summary>
        /// 稿件代码
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(256)]
        public string AppendixCode { get; set; }
        /// <summary>
        /// 稿件的附件= 1
        /// 专审审稿单 = 2
        /// 专家推荐材料 =3
        /// </summary>
        public int Type { get; set; }
        /// <summary>
        /// 初稿 =1
        /// 修订稿=2
        /// 终稿=3
        /// 定稿=9
        /// </summary>
        public int Status { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(256)]
        public string Path { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(512)]
        public string Description { get; set; }
        public DateTime CreateTime { get; set; }
        [Column(TypeName="text")]
        public string Suggestion { get; set; }
        /// <summary>
        /// 稿件格式
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(16)]
        public string Format { get; set; }
        /// <summary>
        /// 稿件大小
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(32)]
        public string Size { get; set; }
        public override T_Appendix Find(System.Linq.Expressions.Expression<Func<T_Appendix, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Appendix.FirstOrDefault(where);
                if (item != null)
                {
                    this.RaletionId = item.RaletionId;
                    this.Own = item.Own;
                    this.CreateTime = item.CreateTime;
                    this.Description = item.Description;
                    this.FileName = item.FileName;
                    this.Id = item.Id;
                    this.Path = item.Path;
                    this.Type = item.Type;
                    this.Status = item.Status;
                    this.Suggestion = item.Suggestion;
                    this.Format = item.Format;
                    this.Size = item.Size;
                    this.AppendixCode = item.AppendixCode;
                    return this;
                }
                return null;
            }
        }
        public override T_Appendix Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }
        public override bool Any(System.Linq.Expressions.Expression<Func<T_Appendix, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Appendix.Any(where);
            }
        }
    }
}
